<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>从零开始搭建一个主流项目框架（三）—RxJava2.0+Retrofit2.0+OkHttp | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="&amp;emsp;&amp;emsp;上一篇，我们把mvp+dagger加进去了，这一篇，我们把网络请求加上 &amp;emsp;&amp;emsp;我这里的网络请求是用的装饰者模式去写的，什么是装饰者模式呢？在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。它是通过创建一个包装对象，也就是装饰来包裹真实的对象。我的理解就是一个接口，两个实现类，一个实现类负责调用接口的方法，另一个类负责功能的具体实现。本文中所提">
<meta property="og:type" content="article">
<meta property="og:title" content="从零开始搭建一个主流项目框架（三）—RxJava2.0+Retrofit2.0+OkHttp">
<meta property="og:url" content="https://haichenyi.com/2018/02/23/从零开始搭建一个主流项目框架（三）—RxJava2-0-Retrofit2-0-OkHttp/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="&amp;emsp;&amp;emsp;上一篇，我们把mvp+dagger加进去了，这一篇，我们把网络请求加上 &amp;emsp;&amp;emsp;我这里的网络请求是用的装饰者模式去写的，什么是装饰者模式呢？在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。它是通过创建一个包装对象，也就是装饰来包裹真实的对象。我的理解就是一个接口，两个实现类，一个实现类负责调用接口的方法，另一个类负责功能的具体实现。本文中所提">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2018-02-24/项目结构.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2018-02-24/配置.png">
<meta property="og:updated_time" content="2019-09-18T09:09:58.435Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="从零开始搭建一个主流项目框架（三）—RxJava2.0+Retrofit2.0+OkHttp">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;上一篇，我们把mvp+dagger加进去了，这一篇，我们把网络请求加上 &amp;emsp;&amp;emsp;我这里的网络请求是用的装饰者模式去写的，什么是装饰者模式呢？在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。它是通过创建一个包装对象，也就是装饰来包裹真实的对象。我的理解就是一个接口，两个实现类，一个实现类负责调用接口的方法，另一个类负责功能的具体实现。本文中所提">
<meta name="twitter:image" content="https://haichenyi.com/uploads/article/2018-02-24/项目结构.png">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-从零开始搭建一个主流项目框架（三）—RxJava2-0-Retrofit2-0-OkHttp" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      从零开始搭建一个主流项目框架（三）—RxJava2.0+Retrofit2.0+OkHttp
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2018/02/23/从零开始搭建一个主流项目框架（三）—RxJava2-0-Retrofit2-0-OkHttp/" class="article-date">
  <time datetime="2018-02-23T12:40:26.000Z" itemprop="datePublished">2018-02-23</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android-框架/">Android -框架</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <p>&emsp;&emsp;上一篇，我们把mvp+dagger加进去了，这一篇，我们把网络请求加上</p>
<p>&emsp;&emsp;我这里的网络请求是用的装饰者模式去写的，什么是装饰者模式呢？在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。它是通过创建一个包装对象，也就是装饰来包裹真实的对象。我的理解就是一个接口，两个实现类，一个实现类负责调用接口的方法，另一个类负责功能的具体实现。本文中所提到的代码都是伪代码，最后会给出完整的，最初版本的项目框架。不包含任何业务逻辑</p>
<a id="more"></a>
<p><img src="/uploads/article/2018-02-24/项目结构.png" alt="项目结构.png"></p>
<p>&emsp;&emsp;容我一个一个来说，首先，我们一般请求网络的时候，会有统一的返回数据格式，一个是需要判断返回code码的，就比方说登录功能，那登录成功，还是失败，我们只用判断code码即可，这种类型，我们统一是HttpNoResult。还有一个是返回数据的，就比方说查一个列表数据。这里我们统一的是HttpResult。我先给出这两个类的代码：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:没有解析数据的返回</span><br><span class="line"> */</span><br><span class="line">public class HttpNoResult &#123;</span><br><span class="line">  private int code;</span><br><span class="line">  private String msg;</span><br><span class="line"></span><br><span class="line">  public int getCode() &#123;</span><br><span class="line">    return code;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public HttpNoResult setCode(int code) &#123;</span><br><span class="line">    this.code = code;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public String getMsg() &#123;</span><br><span class="line">    return msg;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public HttpNoResult setMsg(String msg) &#123;</span><br><span class="line">    this.msg = msg;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public String toString() &#123;</span><br><span class="line">    return &quot;HttpNoResult&#123;&quot; + &quot;code=&quot; + code + &quot;, msg=&apos;&quot; + msg + &apos;\&apos;&apos; + &apos;&#125;&apos;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">import com.google.gson.annotations.SerializedName;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:有解析数据的返回</span><br><span class="line"> */</span><br><span class="line">public class HttpResult&lt;T&gt; &#123;</span><br><span class="line">  private int code;</span><br><span class="line">  private String msg;</span><br><span class="line">  @SerializedName(value = &quot;result&quot;)</span><br><span class="line">  private T data;</span><br><span class="line"></span><br><span class="line">  public int getCode() &#123;</span><br><span class="line">    return code;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public HttpResult setCode(int code) &#123;</span><br><span class="line">    this.code = code;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public String getMsg() &#123;</span><br><span class="line">    return msg;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public HttpResult setMsg(String msg) &#123;</span><br><span class="line">    this.msg = msg;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public T getData() &#123;</span><br><span class="line">    return data;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public HttpResult setData(T data) &#123;</span><br><span class="line">    this.data = data;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public String toString() &#123;</span><br><span class="line">    return &quot;HttpResult&#123;&quot; + &quot;code=&quot; + code + &quot;, msg=&apos;&quot; + msg + &apos;\&apos;&apos; + &quot;, data=&quot; + data + &apos;&#125;&apos;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;这里我就需要说一点，有数据返回的时候，每个数据类型都是不一样的，所以，这里我用的泛型传递，不同的数据类型，传不同的bean对象</p>
<p>&emsp;&emsp;言归正传，我们来说说网络请求的一个接口，两个实现类。</p>
<p>一个接口—HttpHelper</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">import io.reactivex.Flowable;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:网络接口，接口参数Token统一处理，方法中不传Token</span><br><span class="line"> */</span><br><span class="line">public interface HttpHelper &#123;</span><br><span class="line">  /**</span><br><span class="line">   * 登录时获取验证码.</span><br><span class="line">   *</span><br><span class="line">   * @param phone 手机号</span><br><span class="line">   * @return &#123;&quot;code&quot;:0&#125;</span><br><span class="line">   */</span><br><span class="line">  Flowable&lt;HttpNoResult&gt; loginCode(String phone);</span><br><span class="line">  /*Flowable&lt;HttpResult&lt;Login&gt;&gt; login(String phone, String code);</span><br><span class="line">  Flowable&lt;HttpResult&lt;List&lt;DiyBean&gt;&gt;&gt; diyKeys(String allId);*/</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;Flowable是RxJava2.0新增的，所以说RxJava完美兼容Retrofit，泛型就是我们需要解析的数据</p>
<ol>
<li><p>loginCode方法是说返回数据，我们只用判断是否是成功还是失败，</p>
</li>
<li><p>login方法是说返回数据是一个Login对象，至于对象是什么内容，那就是和你们后台确认了</p>
</li>
<li><p>diyKeys方法就是说，返回数据是一个list对象，每个list的item是DiyBean对象</p>
</li>
</ol>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.model.http.HttpHelper;</span><br><span class="line">import com.haichenyi.myproject.model.http.HttpNoResult;</span><br><span class="line"></span><br><span class="line">import io.reactivex.Flowable;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:网络请求的实现类</span><br><span class="line"> */</span><br><span class="line">public class DataHelper implements HttpHelper &#123;</span><br><span class="line">  private HttpHelper http;</span><br><span class="line"></span><br><span class="line">  public DataHelper(HttpHelper http) &#123;</span><br><span class="line">    this.http = http;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public Flowable&lt;HttpNoResult&gt; loginCode(String phone) &#123;</span><br><span class="line">    return http.loginCode(phone);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;DataHelper是HttpHelper的实现类，他的唯一作用就是调用接口的方法即可，具体的功能实现是后面一个类，这里需要说明的是这个类的构造方法要public表示，因为他要dagger生成，用private或者protected表示无法生成。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.model.http.api.HttpApi;</span><br><span class="line"></span><br><span class="line">import io.reactivex.Flowable;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc: 网络接口Retrofit实现</span><br><span class="line"> */</span><br><span class="line">public class RetrofitHelper implements HttpHelper&#123;</span><br><span class="line">  private HttpApi httpApi;</span><br><span class="line"></span><br><span class="line">  @Inject</span><br><span class="line">  RetrofitHelper(HttpApi httpApi) &#123;</span><br><span class="line">    this.httpApi = httpApi;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public Flowable&lt;HttpNoResult&gt; loginCode(String phone) &#123;</span><br><span class="line">    return httpApi.loginCode(phone);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;RetrofitHelper类作为HttpHelper接口的实现类，他是具体功能的实现类，为什么说他是具体功能的实现类呢？因为，他是调用HttpApi接口的方法。HttpApi接口是干什么用的呢？</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http.api;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.model.http.HttpNoResult;</span><br><span class="line">import com.haichenyi.myproject.model.http.ProtocolHttp;</span><br><span class="line"></span><br><span class="line">import io.reactivex.Flowable;</span><br><span class="line">import retrofit2.http.Field;</span><br><span class="line">import retrofit2.http.FormUrlEncoded;</span><br><span class="line">import retrofit2.http.POST;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:网络请求接口api</span><br><span class="line"> */</span><br><span class="line">public interface HttpApi &#123;</span><br><span class="line">  /**</span><br><span class="line">   * 登录时获取验证码.</span><br><span class="line">   *</span><br><span class="line">   * @param phone 手机号</span><br><span class="line">   * @return &#123;&quot;code&quot;:0&#125;</span><br><span class="line">   */</span><br><span class="line">  @FormUrlEncoded</span><br><span class="line">  @POST(ProtocolHttp.METHOD_LOGIN_CODE)</span><br><span class="line">  Flowable&lt;HttpNoResult&gt; loginCode(@Field(&quot;phone&quot;) String phone);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>这个就是Retrofit的网络请求的方式，看不懂？这个就是Retrofit的东西了<br>方法注解，包含@GET、@POST、@PUT、@DELETE、@PATH、@HEAD、@OPTIONS、@HTTP。<br>标记注解，包含@FormUrlEncoded、@Multipart、@Streaming。<br>参数注解，包含@Query、@QueryMap、@Body、@Field，@FieldMap、@Part，@PartMap。<br>其他注解，包含@Path、@Header、@Headers、@Url。</p>
<p>这里我们还差一个接口</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public interface ProtocolHttp &#123;</span><br><span class="line">  String HTTP_HOST = &quot;http://xxx.xx.xxx.xxx:8080/app/con/&quot;;</span><br><span class="line">  String HTTP_COMMON = &quot;common/&quot;;</span><br><span class="line">  String METHOD_LOGIN_CODE = HTTP_COMMON + &quot;code&quot;;//登录发送验证码</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;如上，这里需要注意的是不能以”\”结尾，然后就是，跟你们后台商量，格式不要错了，尽量就只有接口名字不同，接口名字前面部分都是一样的。</p>
<p>&emsp;&emsp;到此，这里基本上就说完了，那么有同鞋就会问了，接口定义方法的时候，我们知道该如何写返回数据类型呢？这个我就不知道了，你得问你们后台，根据后台返回的数据类型去写对应的bean类。推荐一个功能PostMan。</p>
<p>&emsp;&emsp;到目前为止，我们都还没有初始化网络请求的参数，这些网络请求的参数在哪里初始化呢？这些参数，我们就只用初始化一次，我们就想到了dagger的全局单例模式，没错，就是这个，我们上一篇写了很多没有用的东西，里面有一个HttpModule</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.di.module;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.di.qualifier.ApiUrl;</span><br><span class="line">import com.haichenyi.myproject.model.http.ProtocolHttp;</span><br><span class="line">import com.haichenyi.myproject.model.http.api.HttpApi;</span><br><span class="line"></span><br><span class="line">import java.util.concurrent.TimeUnit;</span><br><span class="line"></span><br><span class="line">import javax.inject.Singleton;</span><br><span class="line"></span><br><span class="line">import dagger.Module;</span><br><span class="line">import dagger.Provides;</span><br><span class="line">import okhttp3.OkHttpClient;</span><br><span class="line">import retrofit2.Retrofit;</span><br><span class="line">import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;</span><br><span class="line">import retrofit2.converter.gson.GsonConverterFactory;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:网络请求的参数初始化</span><br><span class="line"> */</span><br><span class="line">@Module</span><br><span class="line">public class HttpModule &#123;</span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  OkHttpClient.Builder providesOkHttpHelper() &#123;</span><br><span class="line">//请求读写超时时间</span><br><span class="line">    return new OkHttpClient.Builder()</span><br><span class="line">        .connectTimeout(10, TimeUnit.SECONDS)</span><br><span class="line">        .readTimeout(10, TimeUnit.SECONDS)</span><br><span class="line">        .writeTimeout(10, TimeUnit.SECONDS);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  OkHttpClient provideClient(OkHttpClient.Builder builder) &#123;</span><br><span class="line">    return builder</span><br><span class="line">//        .addInterceptor(new MyHttpInterceptor())</span><br><span class="line">        .build();</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  Retrofit.Builder providesRetrofitBuilder() &#123;</span><br><span class="line">    return new Retrofit.Builder();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  HttpApi provideApi(@ApiUrl Retrofit retrofit) &#123;</span><br><span class="line">    return retrofit.create(HttpApi.class);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  @ApiUrl</span><br><span class="line">  Retrofit providesApiRetrofit(Retrofit.Builder builder, OkHttpClient client) &#123;</span><br><span class="line">    return createRetrofit(builder, client, ProtocolHttp.HTTP_HOST);//这里就是你的网络请求的url</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private Retrofit createRetrofit(Retrofit.Builder builder, OkHttpClient client, String host) &#123;</span><br><span class="line">    return builder.client(client)</span><br><span class="line">        .baseUrl(host)</span><br><span class="line">        .addConverterFactory(GsonConverterFactory.create())//添加gson自动解析，我们不用关</span><br><span class="line">        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())</span><br><span class="line">        .build();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>如上代码，注释写的都有，考过去用就行了</p>
<p>在AppModule里面添加如下代码</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.di.module;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.base.MyApplication;</span><br><span class="line">import com.haichenyi.myproject.model.DataHelper;</span><br><span class="line">import com.haichenyi.myproject.model.http.HttpHelper;</span><br><span class="line">import com.haichenyi.myproject.model.http.RetrofitHelper;</span><br><span class="line"></span><br><span class="line">import javax.inject.Singleton;</span><br><span class="line"></span><br><span class="line">import dagger.Module;</span><br><span class="line">import dagger.Provides;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">@Module</span><br><span class="line">public class AppModule &#123;</span><br><span class="line">  private MyApplication application;</span><br><span class="line"></span><br><span class="line">  public AppModule(MyApplication application) &#123;</span><br><span class="line">    this.application = application;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  DataHelper provideDataHelper(HttpHelper httpHelper) &#123;</span><br><span class="line">    return new DataHelper(httpHelper);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Provides</span><br><span class="line">  @Singleton</span><br><span class="line">  HttpHelper provideHttpHelper(RetrofitHelper retrofitHelper) &#123;</span><br><span class="line">    return retrofitHelper;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>这里都是dagger了生成全局单例对象需要的东西</p>
<p>在AppComponent里面添加如下代码</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.di.component;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.di.module.AppModule;</span><br><span class="line">import com.haichenyi.myproject.di.module.HttpModule;</span><br><span class="line">import com.haichenyi.myproject.model.DataHelper;</span><br><span class="line"></span><br><span class="line">import javax.inject.Singleton;</span><br><span class="line"></span><br><span class="line">import dagger.Component;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">@Singleton</span><br><span class="line">@Component(modules = &#123;AppModule.class, HttpModule.class&#125;)</span><br><span class="line">public interface AppComponent &#123;</span><br><span class="line">  DataHelper getDataHelper();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>在BaseMvpPresenter里面添加如下代码</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.base;</span><br><span class="line"></span><br><span class="line">import io.reactivex.disposables.CompositeDisposable;</span><br><span class="line">import io.reactivex.disposables.Disposable;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public class BaseMvpPresenter&lt;T extends BaseView&gt; implements BasePresenter&lt;T&gt; &#123;</span><br><span class="line">  protected T baseView;</span><br><span class="line">  private CompositeDisposable disposables;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void attachView(T baseView) &#123;</span><br><span class="line">    this.baseView = baseView;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected void addSubscribe(Disposable disposable) &#123;</span><br><span class="line">    if (null == disposables) &#123;</span><br><span class="line">      disposables = new CompositeDisposable();</span><br><span class="line">    &#125;</span><br><span class="line">    disposables.add(disposable);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void detachView() &#123;</span><br><span class="line">    this.baseView = null;</span><br><span class="line">    unSubscribe();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private void unSubscribe() &#123;</span><br><span class="line">    if (null != disposables) &#123;</span><br><span class="line">      disposables.clear();</span><br><span class="line">      disposables = null;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>至此，就全部写完了，关于网络请求的内容。调用方式如下：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.presenter;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.base.BaseMvpPresenter;</span><br><span class="line">import com.haichenyi.myproject.base.MyApplication;</span><br><span class="line">import com.haichenyi.myproject.contract.MainContract;</span><br><span class="line">import com.haichenyi.myproject.model.DataHelper;</span><br><span class="line"></span><br><span class="line">import javax.inject.Inject;</span><br><span class="line"></span><br><span class="line">import io.reactivex.android.schedulers.AndroidSchedulers;</span><br><span class="line">import io.reactivex.schedulers.Schedulers;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public class MainPresenter extends BaseMvpPresenter&lt;MainContract.IView&gt;</span><br><span class="line">    implements MainContract.Presenter &#123;</span><br><span class="line">  private DataHelper dataHelper;</span><br><span class="line">  @Inject</span><br><span class="line">  MainPresenter() &#123;</span><br><span class="line">    dataHelper = MyApplication.getAppComponent().getDataHelper();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void loadData() &#123;</span><br><span class="line">    addSubscribe(dataHelper.loginCode(&quot;134xxxxxxxx&quot;)</span><br><span class="line">        .subscribeOn(Schedulers.io())</span><br><span class="line">        .observeOn(AndroidSchedulers.mainThread())</span><br><span class="line">        .subscribe());</span><br><span class="line">//    baseView.showTipMsg(&quot;加载数据&quot;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>记得在清单文件里面，加上网络权限</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;</span><br></pre></td></tr></table></figure></div>
<p>网络请求，这样调用之后在哪处理呢？我给出我的几个处理的工具类。首先，按如下图设置1.8支持lambda表达式</p>
<p><img src="/uploads/article/2018-02-24/配置.png" alt="配置.png"></p>
<p>然后添加如下几个类</p>
<p>HttpCode </p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2017/12/21</span><br><span class="line"> * Desc: 网络请求状态码</span><br><span class="line"> */</span><br><span class="line">public interface HttpCode &#123;</span><br><span class="line">  /**</span><br><span class="line">   * 成功.</span><br><span class="line">   */</span><br><span class="line">  int SUCCESS = 0;</span><br><span class="line">  /**</span><br><span class="line">   * 参数为空.</span><br><span class="line">   */</span><br><span class="line">  int NO_PARAMETER = 1;</span><br><span class="line">  /**</span><br><span class="line">   * 服务器错误.</span><br><span class="line">   */</span><br><span class="line">  int SERVER_ERR = 3;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>ApiException </p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2017/12/21</span><br><span class="line"> * Desc: 接口异常判断处理</span><br><span class="line"> */</span><br><span class="line">public class ApiException extends Exception &#123;</span><br><span class="line">  private int code;</span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(&quot;unused&quot;)</span><br><span class="line">  public ApiException(int code) &#123;</span><br><span class="line">    this.code = code;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public ApiException(int code, String message) &#123;</span><br><span class="line">    super(message);</span><br><span class="line">    this.code = code;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public int getCode() &#123;</span><br><span class="line">    return code;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public ApiException setCode(int code) &#123;</span><br><span class="line">    this.code = code;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>MyRxUtils </p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line">import io.reactivex.BackpressureStrategy;</span><br><span class="line">import io.reactivex.Flowable;</span><br><span class="line">import io.reactivex.FlowableTransformer;</span><br><span class="line">import io.reactivex.Scheduler;</span><br><span class="line">import io.reactivex.android.schedulers.AndroidSchedulers;</span><br><span class="line">import io.reactivex.schedulers.Schedulers;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2017/12/27</span><br><span class="line"> * Desc:切换线程的工具类</span><br><span class="line"> */</span><br><span class="line">public class MyRxUtils &#123;</span><br><span class="line">  /**</span><br><span class="line">   * 从其他线程转到主线程.</span><br><span class="line">   *</span><br><span class="line">   * @param scheduler Schedulers.io()等等</span><br><span class="line">   * @param &lt;T&gt;       t</span><br><span class="line">   * @return FlowableTransformer</span><br><span class="line">   */</span><br><span class="line">  public static &lt;T&gt; FlowableTransformer&lt;T, T&gt; toMain(Scheduler scheduler) &#123;</span><br><span class="line">    return upstream -&gt; upstream.subscribeOn(scheduler).observeOn(AndroidSchedulers.mainThread());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public static &lt;T&gt; FlowableTransformer&lt;HttpResult&lt;T&gt;, T&gt; handResult() &#123;</span><br><span class="line">    return upstream -&gt; upstream.subscribeOn(Schedulers.io())</span><br><span class="line">        .observeOn(AndroidSchedulers.mainThread())</span><br><span class="line">        .flatMap(tHttpResult -&gt; &#123;</span><br><span class="line">          if (tHttpResult.getCode() == HttpCode.SUCCESS) &#123;</span><br><span class="line">            return /*createData(tHttpResult.data)*/Flowable.just(tHttpResult.getData());</span><br><span class="line">          &#125; else &#123;</span><br><span class="line">            return Flowable.error(new ApiException(tHttpResult.getCode(), tHttpResult.getMsg()));</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private static &lt;T&gt; Flowable&lt;T&gt; createData(final T data) &#123;</span><br><span class="line">    return Flowable.create(e -&gt; &#123;</span><br><span class="line">      e.onNext(data);</span><br><span class="line">      e.onComplete();</span><br><span class="line">    &#125;, BackpressureStrategy.ERROR);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>MySubscriber</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.model.http;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.base.BaseView;</span><br><span class="line">import io.reactivex.subscribers.ResourceSubscriber;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2017/12/21</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public abstract class MySubscriber&lt;T&gt; extends ResourceSubscriber&lt;T&gt; &#123;</span><br><span class="line">  private BaseView baseView;</span><br><span class="line">  private boolean showLoading;</span><br><span class="line"></span><br><span class="line">  public MySubscriber(BaseView baseView) &#123;</span><br><span class="line">    this.baseView = baseView;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public MySubscriber(BaseView baseView, boolean showLoading) &#123;</span><br><span class="line">    this.baseView = baseView;</span><br><span class="line">    this.showLoading = showLoading;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  protected void onStart() &#123;</span><br><span class="line">    super.onStart();</span><br><span class="line">    if (null != baseView &amp;&amp; showLoading) &#123;</span><br><span class="line">      baseView.showLoading();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onError(Throwable t) &#123;</span><br><span class="line">    if (null == baseView) &#123;</span><br><span class="line">      return;</span><br><span class="line">    &#125;</span><br><span class="line">    baseView.hideLoading();</span><br><span class="line">    if (t instanceof ApiException) &#123;</span><br><span class="line">      ApiException apiException = (ApiException) t;</span><br><span class="line">      switch (apiException.getCode()) &#123;</span><br><span class="line">        case HttpCode.NO_PARAMETER:</span><br><span class="line">          baseView.showTipMsg(&quot;参数为空&quot;);</span><br><span class="line">          break;</span><br><span class="line">        case HttpCode.SERVER_ERR:</span><br><span class="line">          baseView.showTipMsg(&quot;服务器错误&quot;);</span><br><span class="line">          break;</span><br><span class="line">        default:</span><br><span class="line">          break;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onComplete() &#123;</span><br><span class="line">    if (null != baseView) &#123;</span><br><span class="line">      baseView.hideLoading();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>这几个类不想多做解释，结合注释，仔细看几遍，就知道是干嘛用的了</p>
<p>加上这几个之后调用方式就变成了以下的方式：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.presenter;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.base.BaseMvpPresenter;</span><br><span class="line">import com.haichenyi.myproject.base.MyApplication;</span><br><span class="line">import com.haichenyi.myproject.contract.MainContract;</span><br><span class="line">import com.haichenyi.myproject.model.DataHelper;</span><br><span class="line">import com.haichenyi.myproject.model.http.HttpNoResult;</span><br><span class="line">import com.haichenyi.myproject.model.http.MyRxUtils;</span><br><span class="line">import com.haichenyi.myproject.model.http.MySubscriber;</span><br><span class="line"></span><br><span class="line">import javax.inject.Inject;</span><br><span class="line"></span><br><span class="line">import io.reactivex.schedulers.Schedulers;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public class MainPresenter extends BaseMvpPresenter&lt;MainContract.IView&gt;</span><br><span class="line">    implements MainContract.Presenter &#123;</span><br><span class="line">  private DataHelper dataHelper;</span><br><span class="line"></span><br><span class="line">  @Inject</span><br><span class="line">  MainPresenter() &#123;</span><br><span class="line">    dataHelper = MyApplication.getAppComponent().getDataHelper();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void loadData() &#123;</span><br><span class="line">    addSubscribe(dataHelper.loginCode(&quot;134xxxxxxxx&quot;)</span><br><span class="line">        .compose(MyRxUtils.toMain(Schedulers.io()))</span><br><span class="line">        .subscribeWith(new MySubscriber&lt;HttpNoResult&gt;(baseView, true) &#123;</span><br><span class="line">          @Override</span><br><span class="line">          public void onNext(HttpNoResult httpNoResult) &#123;</span><br><span class="line"></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;));</span><br><span class="line">//    baseView.showTipMsg(&quot;加载数据&quot;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>完了，完了，终于写完了。</p>
<p><a href="http://download.csdn.net/download/qq_27634797/10256152" target="_blank" rel="noopener">项目链接</a></p>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2018/02/24/从零开始搭建一个主流项目框架（四）—Kotlin-GreenDao3-2/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            从零开始搭建一个主流项目框架（四）—Kotlin+GreenDao3.2
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2018/02/23/从零开始搭建一个主流项目框架（二）—MVP-dagger2/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">从零开始搭建一个主流项目框架（二）—MVP+dagger2</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>